from appium import webdriver
from time import sleep

from colorama import init, Fore

init(autoreset=True)

desired_caps = {
    "platformName": "Android",
    "automationName": "uiautomator2",
    "platformVersion": "9",
    "deviceName": '2025061700001872',
    "appPackage": "com.seven.qaml.burstserven",
    "appActivity": "com.seven.qaml.burstserven.ui.MenuActivity",
    'skipServerInstallation': False,
    'skipDeviceInitialization': False,
    "noReset": True
}
appium_server_url = 'http://127.0.0.1:4723'
driver = webdriver.Remote(appium_server_url, desired_caps)

count = 0
while count < 100:
    driver.find_element('id', 'com.seven.qaml.burstserven:id/layout_zuWang').click()
    sleep(5)
    while True:
        if driver.find_element('id', 'com.seven.qaml.burstserven:id/text_sign').text == "准备完成，按‘确定’键开始扫描":
            driver.keyevent(66)
            print("等待扫描结果...")
            break
    while True:
        if driver.find_elements('id', 'com.seven.qaml.burstserven:id/tv_message') != []:
            if driver.find_element('id',
                                   'com.seven.qaml.burstserven:id/tv_message').text == '按F1查看列表，确定键继续':
                print("扫描完成")
                driver.keyevent(66)
                sleep(2)
                break
            else:
                print("扫描结果异常")
    driver.keyevent(4)
    sleep(1)
    driver.keyevent(21)
    sleep(1)
    driver.keyevent(66)
    sleep(5)
    count += 1
    print(Fore.GREEN + f"已完成 {count} 次组网起爆扫描验证")
